package com.darren.framework.entity;


import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PostRemove;
import javax.persistence.Version;
import java.util.Date;


@MappedSuperclass
@EntityListeners(DataEntityListener.class)
public class BaseEntity {


    @Id
    @Column(name = "id", columnDefinition = "bigint comment '主键id'")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty(notes = "主键id")
    private Long id;

    @ApiModelProperty(hidden = true)
    @JSONField(name = "createBy")
    @Column(name = "create_by", updatable = false, columnDefinition = "bigint default null comment '创建这条记录的用户ID'")
    private Long createBy;

    @ApiModelProperty(hidden = true)
    @JSONField(name = "createTime", format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", insertable = false, nullable = true, columnDefinition = "datetime DEFAULT CURRENT_TIMESTAMP comment '创建这条记录的时间'")
    private Date createTime;


    @ApiModelProperty(hidden = true)
    @JSONField(name = "updateBy")
    @Column(name = "update_by", nullable = true, columnDefinition = "bigint default null comment '最近一次修改这条记录的用户ID'")
    private Long updateBy;


    @ApiModelProperty(hidden = true)
    @JSONField(name = "updateTime", format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "update_time", updatable = false, nullable = true, columnDefinition = "datetime DEFAULT null on update current_timestamp comment '最近一次修改这条记录的时间'")
    private Date updateTime;

    /**
     * 数据状态，是否删除：true是已删除；false 是未删除
     */
    @Column(name = "is_delete", columnDefinition = "bit default 0 comment '数据状态，是否删除：1 是已删除；0 是未删除'")
    @ApiModelProperty(hidden = true)
    private boolean delete = false;

    /**
     * 备注
     */
    @ApiModelProperty(notes = "备注")
    @Column(name = "remarks", columnDefinition = "varchar(150) default null comment '备注'")
    private String remarks;

    /**
     * 数据版本号，用于乐观锁
     */
    @Version
    @Column(name = "version", columnDefinition = "bigint default 1 comment '数据版本号，用于乐观锁'")
    private Long version;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(Long createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Long getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(Long updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public boolean isDelete() {
        return delete;
    }

    public void setDelete(boolean delete) {
        this.delete = delete;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    @PostRemove
    public void delete() {

        this.delete = true;
    }
}
